package cn.ttxs.lib.base;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.vondear.rxtool.RxKeyboardTool;
import com.vondear.rxtool.view.RxToast;

import cn.ttxs.lib.R;
import cn.ttxs.lib.tool.ToolStatusbar;
import cn.ttxs.lib.wiget.CusLoading;


public abstract class BaseActivity extends AppCompatActivity {
    protected String TAG = this.getClass().getSimpleName();
    private Dialog dialog;

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ToolStatusbar.setStatusBarTranslucent(BaseActivity.this);
        setContentView(getLayoutId());
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissLoading();
    }

    protected void showLoading(Activity activity) {
        this.dismissLoading();
        dialog = new CusLoading(activity, R.style.CustomDialog, true);
        showDialog(activity, dialog);
    }

    protected void showLoading(Activity activity, boolean canCancel) {
        this.dismissLoading();
        dialog = new CusLoading(activity, R.style.CustomDialog, canCancel);
        showDialog(activity, dialog);
    }

    protected void showLoading(Activity activity, String text, boolean canCancel) {
        this.dismissLoading();
        dialog = new CusLoading(activity, R.style.CustomDialog, text, canCancel);
        showDialog(activity, dialog);
    }

    /**
     * 停止下拉刷新或上拉加载
     *
     * @param refreshLayout
     */
    protected void loadOrRefreshFinish(RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }

    /**
     * 停止下拉刷新
     *
     * @param refreshLayout
     */
    protected void refreshFinish(RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
        }
    }

    /**
     * 停止下拉刷新及loadingdialog
     *
     * @param refreshLayout
     */
    protected void allLoadingFinish(RefreshLayout refreshLayout) {
        dismissLoading();
        refreshFinish(refreshLayout);
    }

    /**
     * 关闭键盘
     *
     * @param editText
     */
    protected void closeKeyBoard(EditText editText) {
        RxKeyboardTool.hideSoftInput(BaseActivity.this, editText);
    }

    private void showDialog(Activity activity, Dialog dialog) {
        if (Build.VERSION.SDK_INT > 16) {
            if (!activity.isFinishing() || !activity.isDestroyed()) {
                dialog.show();
            }
        } else {
            if (!activity.isFinishing()) {
                dialog.show();
            }
        }
    }

    protected void showToast(String str) {
        RxToast.showToastShort(str);
    }

    protected void showEmptyToast() {
        RxToast.showToastShort("斩无数据");
    }

    protected void showMainToast(final String str) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            showToast(str);
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    showToast(str);
                }
            });
        }
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        dismissLoading();
    }


}
